<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="85%" destroy-on-close @closed="$emit('closed')" align-center>
        <el-container>
            <el-aside width="200px" v-loading="showGrouploading">
                <el-container>
                    <el-header>
                        <el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
                    </el-header>
                    <el-main class="nopadding">
                        <el-tree ref="group" class="menu" node-key="id" :data="group" :props="groupProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
                    </el-main>
                </el-container>
            </el-aside>
            <el-container>
                <el-header>
                    <div class="left-panel">
                        <el-button type="primary" @click="back">确定</el-button>
                    </div>
                    <div class="right-panel">
                        <div class="right-panel-search">
                            <el-input v-model="search.goods_name" placeholder="产品型号" clearable @keyup.enter="upsearch"></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                            <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                        </div>
                    </div>
                </el-header>
                <el-main class="nopadding">
                    <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border height="650" :params="query">
                        <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                        <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
                        <el-table-column label="产品型号" prop="goods.goods_name" align="center"></el-table-column>
                        <el-table-column label="产品编码" prop="goods.goods_code" align="center"></el-table-column>
                        <el-table-column label="产品分类" prop="goods.cate.cate_name" align="center"></el-table-column>
                        <el-table-column label="仓库" prop="ware.ware_name" align="center"></el-table-column>
                        <el-table-column label="库存数量" prop="curr_stock" align="center"></el-table-column>
                    </scTable>
                </el-main>
            </el-container>
        </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
    props:{
        query: { type: Object, default: () => ({}) },
    },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择库存商品',
				edit: '编辑商品',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
            apiObj: this.$API.goods.stock,
			selection: [],
			search: {
				goods_name: null,
			},
            data: [],
            showGrouploading: false,
			groupFilterText: '',
			group: [],
            groupProps: {
				label: (data) => {
					return data.cate_name
				}
			},
		}
	},
    watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
        this.getShop()
		this.getGroup()
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// 表单注入数据
		setData(data) {
            this.type = data
		},
        // 选择事件
        selectionChange(selection) {
			this.selection = selection;
            // this.$emit('success', selection)
            // this.visible = false;
		},
        // 选中返回
        back(){
            this.$emit('success', this.selection)
            this.visible = false;
        },
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
		async getShop() {
			var res = await this.$API.procure.plan.shop.get();
            this.data = res.data
		},
        //加载树数据
		async getGroup() {
			this.showGrouploading = true;
			var res = await this.$API.goods.goodsCate.all.get();
			this.showGrouploading = false;
			var allNode = { id: '', cate_name: '所有' }
			res.data.unshift(allNode);
			this.group = res.data;
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.cate_name.indexOf(value) !== -1;
		},
		groupClick(node) {
            this.pid = node.id
			var params = {
				cate_id: node.id,
                allocation: 1,
                ware_id:this.query.ware_id
			}
			this.$refs.table.reload(params)
        },
	}
}
</script>

<style></style>
